﻿@inject IDataCacheService _dataCacheService
@inject IHttpService _httpService

@inject IPopupService PopupService
@if (isReady)
{
    <div class="@Class">
        <div class="pb-2" style="display: flex; justify-content: center;">被合并的词条 -> 合并到的词条</div>
        <MRow Class="mb-4">
            <MCol Cols="12" Md="6">
                <CnGalWebSite.Components.Inputs.MasaAutoComplete @bind-Value="@model.SubName" Label="被合并的词条" AutoCompleteItems="EntryItems" />
            </MCol>
            <MCol Cols="12" Md="6">
                <CnGalWebSite.Components.Inputs.MasaAutoComplete @bind-Value="@model.HostName" Label="合并到的词条" AutoCompleteItems="EntryItems" />
            </MCol>
            <MCol Cols="12">
                <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="添加" OnClick="OnAdd" Icon="mdi-plus" Class="mb-4" />
             </MCol>
        </MRow>


        <MSimpleTable Class="mb-4">
            <thead>
                <tr>
                    <th class="text-left">
                        被合并的词条
                    </th>
                    <th class="text-left">
                        合并到的词条
                    </th>
                    <th class="text-left">
                        操作
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr @key="item.SubName">
                        <td>@item.SubName</td>
                        <td>@item.HostName</td>
                        <td>
                            <CnGalWebSite.Components.Buttons.MasaButton Text="删除" TextStyle OnClick="@(()=>OnDelete(item))" Icon="mdi-trash-can" Color="error" />
                        </td>
                    </tr>
                }
            </tbody>
        </MSimpleTable>

        <CnGalWebSite.Components.Buttons.MasaButton Text="下一步" Rounded Icon="mdi-arrow-down-bold-outline" OnClick="OnClickFinshInput" Class="mb-2" />
    </div>

}
else
{
    <CnGalWebSite.Components.Progressings.ProgressingCard />
}


@code {


    [Parameter]
    public List<MergeEntryModel> Model { get; set; }
    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public EventCallback OnFinshInput { get; set; }

    MergeEntryModel model = new MergeEntryModel();
    private bool isReady = false;
    public List<string> EntryItems { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override async Task OnInitializedAsync()
    {
        try
        {
            //获取所有词条
            EntryItems = await _httpService.GetAsync<List<string>>("api/home/GetSearchTipList");
            isReady = true;
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取所有词条名称失败");
        }
    }

    public async Task OnAdd()
    {
        if (string.IsNullOrWhiteSpace(model.SubName) || string.IsNullOrWhiteSpace(model.HostName))
        {
            await PopupService.ToastAsync("先填写词条名称哦", AlertTypes.Error);
            return;
        }
        if (Model.Any(s => s.SubName == model.SubName))
        {
            await PopupService.ToastAsync("被合并的词条只能添加一次", AlertTypes.Error);
            return;
        }
        if (model.SubName == model.HostName)
        {
            await PopupService.ToastAsync("不能合并同一个词条哦", AlertTypes.Error);
            return;
        }
        Model.Add(model);
        model = new MergeEntryModel();
    }

    public void OnDelete(MergeEntryModel model)
    {
        Model.Remove(model);
        
    }

    public async Task OnClickFinshInput()
    {
        if (Model.Count == 0)
        {
            await PopupService.ToastAsync("你还没有填写合并的词条哦", AlertTypes.Error);
            return;
        }
        Model.ForEach(s => s.CompleteTaskCount = 0);
        await OnFinshInput.InvokeAsync();
    }
}
